<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item class="w-[280px]" label="主体名称">
                    <el-input v-model="queryParams.entity_name" placeholder="请输入主体名称" clearable
                        @keyup.enter="resetPage" />
                </el-form-item>
                <el-form-item label="提交时间">
                    <daterange-picker v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <export-data class="ml-2.5" :fetch-fun="getProcessList" :params="queryParams"
                        :page-size="pager.size" />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table size="large" v-loading="pager.loading" :data="pager.lists" style="width: 100%">
                <el-table-column label="提交人手机号" prop="user_account" min-width="120" />
                <el-table-column label="主体名称" prop="entity_name" min-width="100" />
                <el-table-column label="统一社会信用代码" prop="credit_code" min-width="100" />
                <el-table-column label="成立日期" prop="start_date" min-width="100" />
                <el-table-column label="法定代表人姓名" prop="oper_name" min-width="100" />
                <el-table-column label="状态" prop="status" min-width="100" />
                <el-table-column label="注册号" prop="no" min-width="100" />
                <el-table-column label="地址" prop="comp_address" min-width="100" />
                <el-table-column label="所在地区" prop="region" min-width="120" />
                <el-table-column label="经度" prop="longitude" min-width="120" />
                <el-table-column label="纬度" prop="latitude" min-width="120" />
                <el-table-column label="去年产值(万元)" prop="last_year_output" min-width="120" />
                <el-table-column label="本年预计产值(万元)" prop="current_year_estimate" min-width="120" />
                <el-table-column label="已完成产值(万元)" prop="completed_output" min-width="120" />
                <el-table-column label="主要产品类型" prop="main_product_type" min-width="120" />
                <el-table-column label="原材料转化量(吨)" prop="material_conversion" min-width="120" />
                <el-table-column label="销售方式" prop="sales_method" min-width="120" />
                <el-table-column label="主销区" prop="main_sales_region" min-width="120" />
                <el-table-column label="出口目的地" prop="export_destination" min-width="120" />
                <el-table-column label="出口额(万元)" prop="export_value" min-width="120" />
                <el-table-column label="联系人" prop="contact_person" min-width="120" />
                <el-table-column label="联系电话" prop="contact_phone" min-width="120" />
                <el-table-column label="备注" prop="remark" min-width="120" />
                <el-table-column label="主营产品照片" min-width="200">
                    <template #default="{ row }">
                        <div v-if="row.product_images">
                            <el-image v-for="(image, index) in row.product_images.split(',')" :key="index"
                                :src="config.baseUrl + image" fit="cover"
                                style="width: 50px; height: 50px; margin-right: 10px"
                                @click="openImageDialog(row.product_images.split(','), index)" />
                        </div>
                        <span v-else style="color: red">无图片</span>
                    </template>
                </el-table-column>
                <el-table-column label="生产环境照片" min-width="200">
                    <template #default="{ row }">
                        <div v-if="row.production_images">
                            <el-image v-for="(image, index) in row.production_images.split(',')" :key="index"
                                :src="config.baseUrl + image" fit="cover"
                                style="width: 50px; height: 50px; margin-right: 10px"
                                @click="openImageDialog(row.production_images.split(','), index)" />
                        </div>
                        <span v-else style="color: red">无图片</span>
                    </template>
                </el-table-column>
                <el-table-column label="提交时间" prop="create_time" min-width="120" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['user.user/detail']" type="primary" link>
                            <router-link :to="{
                                path: getRoutePath('user.user/detail'),
                                query: {
                                    id: row.id
                                }
                            }">
                                详情
                            </router-link>
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog v-model="dialogVisible" :title="'图片查看'" width="50%">
                <el-carousel v-if="dialogImageUrl.length" v-model="currentImageIndex" arrow="always" :autoplay="false">
                    <el-carousel-item v-for="(image, index) in dialogImageUrl" :key="index">
                        <el-image :src="image" fit="contain" style="width: 100%; height: 200px" />
                    </el-carousel-item>
                </el-carousel>
                <template #footer>
                    <el-button @click="dialogVisible = false">关闭</el-button>
                </template>
            </el-dialog>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="consumerLists">
import config from '@/config'
import { ref, reactive, onActivated } from 'vue'
import { getProcessList } from '@/api/Processed'
import { ClientMap } from '@/enums/appEnums'
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'

const formRef = ref(null)
const queryParams = reactive({
    entity_name: '',
    channel: '',
    start_time: '',
    end_time: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getProcessList,
    params: queryParams
})

// 弹窗是否显示
const dialogVisible = ref(false)
// 当前查看的图片 URL
const dialogImageUrl = ref<string[]>([])
// 当前图片索引
const currentImageIndex = ref(0)

// 打开图片浏览工具
const openImageDialog = (images: string[], index: number) => {
    // 优化：避免重复调用 split 方法
    const imageList = images.map(img => img.trim()).filter(img => img)
    dialogImageUrl.value = imageList.map(img => config.baseUrl + img)
    currentImageIndex.value = index
    dialogVisible.value = true
}

// 组件激活时获取列表数据
onActivated(() => {
    getLists()
})

// 初始化获取列表数据
getLists()

</script>
